package net.sdd.school.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sdd.common.result.Result;
import net.sdd.school.entity.Chapter;
import net.sdd.school.entity.vo.ChapterVo;
import net.sdd.school.service.IChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/sddonline/chapter")
@Api(tags = "章节管理")
public class ChapterController {
    @Autowired
    private IChapterService chapterService;

    /**
     * 根据课程id，查询章节还有视频信息
     */
    @GetMapping("/video/{courseId}")
    @ApiOperation("根据课程id查询章节视频")
    public Result findChapterAndVideo(@PathVariable("courseId") String courseId) {
        List<ChapterVo> list = chapterService.findChapterAndVideo(courseId);
        return Result.ok().data("chapterList",list);
    }

    @ApiOperation("根据id查询章节")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")String id) {
        Chapter chapter = chapterService.getById(id);
        return Result.ok().data("chapter",chapter);
    }

    @ApiOperation("修改章节")
    @PutMapping
    public Result update(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return Result.ok();
    }

    @ApiOperation("添加章节")
    @PostMapping
    public Result add(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return Result.ok();
    }


    @ApiOperation("删除章节")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")String id) {
        chapterService.deleteChapterById(id);
        return Result.ok();
    }
}

